/*
 * Created by JFormDesigner on Sun Jun 30 14:19:22 CST 2024
 */

package scct.hys.ui;

import scct.hys.dao.CinemaDao;
import scct.hys.dao.HallDao;
import scct.hys.model.Cinema;
import scct.hys.model.Hall;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 26984
 */
public class PanelCinema extends JPanel {
    FormMain main;
    DefaultTableModel model;
    public PanelCinema(FormMain main) {
        initComponents();
        initView();
        this.main=main;
    }

    private void initView() {
        model= (DefaultTableModel) tabCinema.getModel();
        model.addColumn("id");
        model.addColumn("影院名称");
        model.addColumn("影院地址");
        model.addColumn("总座位数");
        getTable();
       

    }

    private void getTable() {
        CinemaDao cinemaDao=new CinemaDao();
        List<Cinema> allCinemas=cinemaDao.getAllCinemas();
        for (Cinema allcinema:allCinemas){
            model.addRow(new Object[]{allcinema.getId(),allcinema.getName(),allcinema.getAddress(),allcinema.getTotalSeats()});
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgCinemaAdd dlgAddCinema= new DlgCinemaAdd(main);
        dlgAddCinema.setVisible(true);
        dlgAddCinema.setModal(true);
        getTable();
    }

  

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        if (tabCinema.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"请选择修改对象" );
            return;
        }
        int index=tabCinema.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        DlgCinemaUpdate dlgCinemaUpdate=new DlgCinemaUpdate(main,id);
        dlgCinemaUpdate.setModal(true);
        dlgCinemaUpdate.setVisible(true);
        getTable();
        
        
    }
    private void btnDel(ActionEvent e) {
        // TODO add your code here
        if (tabCinema.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this,"删除成功");
            return;
        }
        int index=tabCinema.getSelectedRow();
        int id=Integer.parseInt(model.getValueAt(index,0).toString());
        CinemaDao cinemaDao=new CinemaDao();
        cinemaDao.delCinema(id);
        getTable();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnDel = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabCinema = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing. border. EmptyBorder(
        0, 0, 0, 0) , "JFor\u006dDesi\u0067ner \u0045valu\u0061tion", javax. swing. border. TitledBorder. CENTER, javax. swing. border. TitledBorder
        . BOTTOM, new java .awt .Font ("Dia\u006cog" ,java .awt .Font .BOLD ,12 ), java. awt. Color.
        red) , getBorder( )) );  addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java .
        beans .PropertyChangeEvent e) {if ("bord\u0065r" .equals (e .getPropertyName () )) throw new RuntimeException( ); }} );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 5));

            //---- btnAdd ----
            btnAdd.setText("\u6dfb\u52a0\u5f71\u9662");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnDel ----
            btnDel.setText("\u5220\u9664");
            btnDel.addActionListener(e -> btnDel(e));
            panel1.add(btnDel);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabCinema);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    private void btnCancel(ActionEvent e) {
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnDel;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabCinema;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
